package com.example.mywiseelves.service;

import android.accessibilityservice.AccessibilityService;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.view.accessibility.AccessibilityEvent;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.example.mywiseelves.R;
import com.example.mywiseelves.bean.ElvesBean;
import com.example.mywiseelves.bean.VoiceBean;
import com.example.mywiseelves.dao.VoicesDao;
import com.example.mywiseelves.utils.FloatWindowManager;
import com.example.mywiseelves.view.ElvesView;

/**
 * 控制精灵互动服务
 * */
public class ElvesViewService extends Service {
    ElvesView elvesView;
    FloatWindowManager floatWindowManager;
    ElvesBean elvesBean;
    VoicesDao voicesDao;
    private String openBrowser;
    private String openPhone;

    @Override
    public void onCreate() {
        super.onCreate();
        voicesDao=new VoicesDao(this);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        elvesBean = (ElvesBean) intent.getSerializableExtra("elves_data");
        openBrowser=voicesDao.voicesQuery(0);
        openPhone=voicesDao.voicesQuery(1);
        startElver();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        floatWindowManager.removeElvesView(this);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public void startElver(){
        //清空
        elvesView=new ElvesView(this);
        elvesView.setNormalAnimId(elvesBean.getElves_anim_normal());
        elvesView.setEatAnimId(elvesBean.getElves_anim_click());
        elvesView.setVoice(elvesBean.getElves_wisdom());
        elvesView.setOpenBrowser(openBrowser);
        elvesView.setOpenPhone(openPhone);
        floatWindowManager=new FloatWindowManager(elvesView);
        floatWindowManager.addElvesView(this);
    }
}